/*
 * @Author: EltonLi lijiaming@uavi-tech.com
 * @Date: 2024-04-19 15:48:45
 * @LastEditors: lvgrape lvgrape@outlook.com
 * @LastEditTime: 2024-05-15 15:25:10
 * @FilePath: \PREMIUM_RTThread_V1.0\ZINO\iwdg\iwdg.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <rtconfig.h>
#include <rtthread.h>
#include <rtdevice.h>
// #include "drv_wdt.h"
#include "zino.h"

#define IWDG_DEVICE_NAME "wdt" /* 看门狗设备名称 */
static rt_device_t wdg_dev;    /* 看门狗设备句柄 */
void idle_hook(void)
{
    /* 在空闲线程的回调函数里喂狗 */
    rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
    // rt_kprintf("feed the dog!\n ");
}
static int iwdg_sample()
{
    rt_err_t ret = RT_EOK;
    rt_uint32_t timeout = 20; /* 溢出时间 */
    // char device_name[RT_NAME_MAX];
    /* 判断命令行参数是否给定了设备名称 */
    /* 根据设备名称查找看门狗设备，获取设备句柄 */
    wdg_dev = rt_device_find(IWDG_DEVICE_NAME);
    if (!wdg_dev)
    {
        rt_kprintf("find %s failed!\n", IWDG_DEVICE_NAME);
        return RT_ERROR;
    }
    /* 初始化设备 */
    ret = rt_device_init(wdg_dev);
    if (ret != RT_EOK)
    {
        rt_kprintf("initialize %s failed!\n", IWDG_DEVICE_NAME);
        return RT_ERROR;
    }
    /* 设置看门狗溢出时间 */
    ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
    if (ret != RT_EOK)
    {
        rt_kprintf("set %s timeout failed!\n", IWDG_DEVICE_NAME);
        return RT_ERROR;
    }
    /* 设置空闲线程回调函数 */
    // rt_thread_idle_sethook(idle_hook);
    return ret;
}
/* 导出到 msh 命令列表中 */
ZINO_APP_EXPORT(iwdg_sample);

